from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from .routers import auth, users, avatars, friends, posts, chat, ai
from .database import Base, engine
import uvicorn
import os

# 创建数据库表
Base.metadata.create_all(bind=engine)

app = FastAPI(
    title="AvatarChat API",
    description="基于多模态LLM的数字分身预匹配社交系统API",
    version="1.0.0"
)

# # 确保uploads目录存在
# os.makedirs("uploads/appearances", exist_ok=True)

# 配置静态文件服务
app.mount("/uploads", StaticFiles(directory="uploads"), name="uploads")

# 配置CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=[
        "http://localhost:5508",
        "http://127.0.0.1:5508",
        "http://localhost:5500",
        "http://127.0.0.1:5500",
        "http://localhost:8000",  # Swagger UI
        "http://127.0.0.1:8000",   # Swagger UI
        "http://localhost:8001",
        "http://127.0.0.1:8001"
    ],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
    expose_headers=["*"]
)

# 注册路由
app.include_router(auth.router, prefix="/api", tags=["认证"])
app.include_router(users.router, prefix="/api/users", tags=["用户"])
app.include_router(avatars.router, prefix="/api/avatars", tags=["化身"])
app.include_router(friends.router, prefix="/api/friends", tags=["好友"])
app.include_router(posts.router, prefix="/api/posts", tags=["帖子"])
app.include_router(chat.router, prefix="/api/chat", tags=["聊天"])
app.include_router(ai.router, prefix="/api/ai", tags=["AI"])

if __name__ == "__main__":
    uvicorn.run("app.main:app", host="127.0.0.1", port=8000, reload=True)